﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RTEditor;
public class GenAnchorGroup : MonoBehaviour, IRTEditorEventListener
{
    public float offset = 0.5f;
    public GameObject anchorGroupGo;
    public GameObject anchorGo;
    private bool isFirstClick = true;

    void Start()
    {

    }
    public void OnAlteredByTransformGizmo(Gizmo gizmo)
    {

    }

    public bool OnCanBeSelected(ObjectSelectEventArgs selectEventArgs)
    {
        return true;
    }

    public void OnDeselected(ObjectDeselectEventArgs deselectEventArgs)
    {
        
    }

    public void OnSelected(ObjectSelectEventArgs selectEventArgs)
    {
        Debug.Log("clicked!!");
        if (isFirstClick)
        {
            GenerateBounds();
            StartCoroutine(GenerateAnchors());
            isFirstClick = false;
        }
        
    }

    private void GenerateBounds()
    {
        Transform parent = EditorObjectSelection.Instance.LastSelectedGameObject.transform;
        Vector3 postion = parent.position;
        Quaternion rotation = parent.rotation;
        Vector3 scale = parent.localScale;
        parent.position = Vector3.zero;
        parent.rotation = Quaternion.Euler(Vector3.zero);
        parent.localScale = Vector3.one;

        Collider[] colliders = parent.GetComponentsInChildren<Collider>();
        foreach (Collider child in colliders)
        {
            DestroyImmediate(child);
        }

        Vector3 center = Vector3.zero;
        Renderer[] renders = parent.GetComponentsInChildren<Renderer>();
        foreach (Renderer child in renders)
        {
            center += child.bounds.center;
        }
        center /= parent.childCount;
        Bounds bounds = new Bounds(center, Vector3.zero);
        foreach (Renderer child in renders)
        {
            bounds.Encapsulate(child.bounds);
        }
        BoxCollider boxCollider = parent.gameObject.AddComponent<BoxCollider>();
        boxCollider.center = bounds.center - parent.position;
        boxCollider.size = bounds.size;

        parent.position = postion;
        parent.rotation = rotation;
        parent.localScale = scale;

        //Debug.Log(boxCollider.bounds.min);
        
        //Debug.Log(boxCollider.bounds.max);
        //GameObject go2 = Instantiate(anchorGo, boxCollider.bounds.max + bounds.extents.normalized, default, transform);

    }

    /// <summary>
    /// 生成锚点组别
    /// </summary>
    /// <returns></returns>
    IEnumerator GenerateAnchors()
    {
        yield return new WaitForFixedUpdate();
        Bounds bounds = GetComponent<Collider>().bounds;
        Vector3 letfBottom = bounds.min;
        Vector3 leftTop = bounds.min + new Vector3(0, 0, bounds.size.z);
        Vector3 rightBottom = bounds.min + new Vector3(bounds.size.x, 0, 0);
        Vector3 rightTop = bounds.max - new Vector3(0, bounds.size.y, 0);

        //生成AnchorGroup
        GameObject group = Instantiate(anchorGroupGo, bounds.center, default, transform);
        AnchorGroup000 groupScript = group.GetComponent<AnchorGroup000>();
        

        //生成锚点
        //GameObject go1 = Instantiate(anchorGo, letfBottom - Vector3.up, default);
        //go1.transform.SetParent(group.transform);
        //go1.GetComponent<Anchor>().SetOrder(0);
        //GameObject go2 = Instantiate(anchorGo, leftTop - Vector3.up, default);
        //go2.transform.SetParent(group.transform);
        //go2.GetComponent<Anchor>().SetOrder(1);
        //GameObject go3 = Instantiate(anchorGo, rightBottom - Vector3.up, default);
        //go3.transform.SetParent(group.transform);
        //go3.GetComponent<Anchor>().SetOrder(2);
        //GameObject go4 = Instantiate(anchorGo, rightTop - Vector3.up, default);
        //go4.transform.SetParent(group.transform);
        //go4.GetComponent<Anchor>().SetOrder(3);


    }
}
